package com.ycy.leetcode;

import org.junit.Test;

import java.util.Arrays;

public class WuXuArrMiss {

  /**
   * 连续的无序数组中，查找缺少值
   */
  public static void main(String[] args) {

    int[] arr = new int[]{4, 2, 1, 5, 6};
    double x = ((double) (arr.length) / 2) * (arr.length + 1);
    System.out.println(x);

    int num = 0;
    for (int i = 0; i < arr.length; i++) {
      num += arr[i];
    }

    System.out.println(num - x);

  }

  @Test
  public void test() {
//  在一个无序数组中有n个连续的数字，缺少了其中一个，如何找出来
//  [3,2,1,5,6,9,8,7]

    int[] arr = new int[]{3, 2, 1, 5, 6, 9, 8, 7, -1};

    //
    for (int i = 0; i < arr.length; i++) {
      int tar = arr[i];
      int a = tar - 1;
      while (a != i && arr[i] != -1) {
        int tmp = arr[i];
        arr[i] = arr[a];
        arr[a] = tmp;
        a = arr[i] - 1;
      }
    }
    System.out.println(Arrays.toString(arr));
  }


}
